import 'dart:convert';
import 'package:flutter/material.dart';
import './answersjiewei.dart';
import 'package:dio/dio.dart';
import 'package:timer_builder/timer_builder.dart';
import 'dart:math';

Dio dio = new Dio();

class Answers extends StatefulWidget {
  String title;
  Answers({Key key, this.title = '答题'}) : super(key: key);

  @override
  _AnswersState createState() => _AnswersState(this.title);
}

class _AnswersState extends State<Answers> {
  String title;
  _AnswersState(String title) {
    this.title = title;
  }

  Response response;

  var contain; //内容
  var a,b,c,d; //a b c d
  var answer; //答案
  int rightanswer = 0; // 记录答对的题数
  int _count = 1; //  1/5
  bool answerA,answerB,answerC,answerD; //  对或错 做出相应颜色变化
  bool answerSUM; //   控制下一题和答案显示
  bool answerSUMA,answerSUMB,answerSUMC,answerSUMD; //  控制正常状态与点击状态的切换
  var xuananswer; //  选择哪个做比对
  bool kaiguan = false; //  开关控制不能多选
  int i = 0;    // 答题时间
  int lengthti;  //题库长度
  int position; //随机位置
  void mytext() async {
    // 判断从哪个答题区域进来拉取相应数据
    if (title == "每日答题") {
      response = await dio
          .get('http://xssw.gdit.edu.cn/sizheng/api/v1/issue?query={"mold":"每日答题"}');
          lengthti=response.data.length;
          position = Random().nextInt(lengthti);
    } else if (title == '每周答题') {
      response = await dio
          .get('http://xssw.gdit.edu.cn/sizheng/api/v1/issue?query={"mold":"每周答题"}');
          lengthti=response.data.length;
          position = Random().nextInt(lengthti);
    } else if (title == '专项答题') {
      response = await dio
          .get('http://xssw.gdit.edu.cn/sizheng/api/v1/issue?query={"mold":"专项答题"}');
          lengthti=response.data.length;
          position = Random().nextInt(lengthti);
    } else if (title == '挑战答题') {
      response = await dio
          .get('http://xssw.gdit.edu.cn/sizheng/api/v1/issue?query={"mold":"挑战答题"}');
          lengthti=response.data.length;
          position = Random().nextInt(lengthti);
    }
    setState(() {
      // 首次赋值
      contain = response.data[position]['contain'];
      a = response.data[position]['option']['A'];
      b = response.data[position]['option']['B'];
      c = response.data[position]['option']['C'];
      d = response.data[position]['option']['D'];
      answer = response.data[position]['answer'];
    });
  }
  @override
  void initState() {
    super.initState();
    mytext(); // 页面启动拉取值
  }

  void next() {
    // 点击下一题时进行数据更新赋值
    if (_count <= response.data.length) {
      setState(() {
        _count = _count + 1;
        position = Random().nextInt(lengthti);
        contain = response.data[position]['contain'];
        a = response.data[position]['option']['A'];
        b = response.data[position]['option']['B'];
        c = response.data[position]['option']['C'];
        d = response.data[position]['option']['D'];
        answer = response.data[position]['answer'];
        answerA = null; // 重新赋值
        answerB = null;
        answerC = null;
        answerD = null;
        answerSUM = null;
        xuananswer = null;
        answerSUMA = null;
        answerSUMB = null;
        answerSUMC = null;
        answerSUMD = null;
        kaiguan = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('${title}'),
        centerTitle: true,
        actions: <Widget>[
          answerSUM == true
              ? Container(
                  width: 80,
                  margin: EdgeInsets.fromLTRB(0, 15, 10, 10),
                  child: RaisedButton(
                    color: Colors.brown,
                    child: Text(
                      '下一题',
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: next,
                  ),
                )
              : Text('')
        ],
      ),
      body: Stack(
        children: <Widget>[
          Image.asset('images/shijian4.jpg'),
          ListView(
            children: <Widget>[
              _count < 6
                  ? Container(
                      //答题区
                      margin: EdgeInsets.fromLTRB(20, 60, 20, 0),
                      decoration: BoxDecoration(
                          color: Colors.white,
                          border: Border.all(
                              width: 1,
                              color: Color.fromRGBO(220, 220, 220, 1))),
                      child: Column(
                        children: <Widget>[
                          SizedBox(
                            height: 10,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Container(
                                margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 3,
                                      height: 18,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      color: Colors.red,
                                    ),
                                    Text('单选题',
                                        style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            fontSize: 18)),
                                  ],
                                ),
                              ),
                              Container(
                                margin: EdgeInsets.fromLTRB(0, 0, 10, 0),
                                child: Row(
                                  children: <Widget>[
                                    Text('${_count}',
                                        style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            fontSize: 36)),
                                    Text(
                                      '/5',
                                      style: TextStyle(
                                          color:
                                              Color.fromRGBO(175, 175, 175, 1)),
                                    )
                                  ],
                                ),
                              )
                            ],
                          ),
                          Divider(),
                          Container(
                            margin: EdgeInsets.fromLTRB(13, 5, 13, 0),
                            child: Text('${contain}'),
                          ),
                          SizedBox(
                            height: 50,
                          ),
                          Container(
                              width: 280,
                              height: 45,
                              alignment: Alignment.centerLeft,
                              decoration: BoxDecoration(
                                color: Color.fromRGBO(250, 251, 252, 1),
                                border: Border.all(
                                  color: Color.fromRGBO(220, 220, 220, 1),
                                ),
                                borderRadius:
                                    BorderRadius.all(Radius.circular(5)),
                              ),
                              child: InkWell(
                                child: answerSUMA != null
                                    ? Row(
                                        children: <Widget>[
                                          SizedBox(
                                            width: 13,
                                          ),
                                          Text('A.',
                                              style: answerA == true
                                                  ? TextStyle(
                                                      color: Colors.green)
                                                  : TextStyle(
                                                      color: Colors.red)),
                                          SizedBox(
                                            width: 5,
                                          ),
                                          Text(
                                            '${a}',
                                            style: answerA == true
                                                ? TextStyle(color: Colors.green)
                                                : TextStyle(color: Colors.red),
                                          ),
                                        ],
                                      )
                                    : Row(
                                        children: <Widget>[
                                          SizedBox(
                                            width: 13,
                                          ),
                                          Text('A.',
                                              style: TextStyle(
                                                  color: Color.fromRGBO(
                                                      121, 128, 136, 1))),
                                          SizedBox(
                                            width: 5,
                                          ),
                                          Text(
                                            '${a}',
                                            style: TextStyle(
                                                color: Color.fromRGBO(
                                                    121, 128, 136, 1)),
                                          ),
                                        ],
                                      ),
                                onTap: () {
                                  if (kaiguan == false) {
                                    kaiguan = true;
                                    xuananswer = 'A';
                                    answerSUM = true;
                                    answerSUMA = true;
                                    if (answer == xuananswer) {
                                      answerA = true;
                                      rightanswer = rightanswer + 1;
                                    } else {
                                      answerA = false;
                                    }
                                  }
                                  setState(() {});
                                },
                              )),
                          SizedBox(
                            height: 10,
                          ),
                          Container(
                            width: 280,
                            height: 45,
                            alignment: Alignment.centerLeft,
                            decoration: BoxDecoration(
                              color: Color.fromRGBO(250, 251, 252, 1),
                              border: Border.all(
                                color: Color.fromRGBO(220, 220, 220, 1),
                              ),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(5)),
                            ),
                            child: InkWell(
                              child: answerSUMB != null
                                  ? Row(
                                      children: <Widget>[
                                        SizedBox(
                                          width: 13,
                                        ),
                                        Text(
                                          'B.',
                                          style: answerB == true
                                              ? TextStyle(color: Colors.green)
                                              : TextStyle(color: Colors.red),
                                        ),
                                        SizedBox(
                                          width: 5,
                                        ),
                                        Text(
                                          '${b}',
                                          style: answerB == true
                                              ? TextStyle(color: Colors.green)
                                              : TextStyle(color: Colors.red),
                                        ),
                                      ],
                                    )
                                  : Row(
                                      children: <Widget>[
                                        SizedBox(
                                          width: 13,
                                        ),
                                        Text(
                                          'B.',
                                          style: TextStyle(
                                              color: Color.fromRGBO(
                                                  121, 128, 136, 1)),
                                        ),
                                        SizedBox(
                                          width: 5,
                                        ),
                                        Text(
                                          '${b}',
                                          style: TextStyle(
                                              color: Color.fromRGBO(
                                                  121, 128, 136, 1)),
                                        ),
                                      ],
                                    ),
                              onTap: () {
                                if (kaiguan == false) {
                                  kaiguan = true;
                                  xuananswer = 'B';
                                  answerSUM = true;
                                  answerSUMB = true;
                                  if (answer == xuananswer) {
                                    answerB = true;
                                    rightanswer = rightanswer + 1;
                                  } else {
                                    answerB = false;
                                  }
                                }
                                setState(() {});
                              },
                            ),
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Container(
                              width: 280,
                              height: 45,
                              alignment: Alignment.centerLeft,
                              decoration: BoxDecoration(
                                color: Color.fromRGBO(250, 251, 252, 1),
                                border: Border.all(
                                  color: Color.fromRGBO(220, 220, 220, 1),
                                ),
                                borderRadius:
                                    BorderRadius.all(Radius.circular(5)),
                              ),
                              child: InkWell(
                                child: answerSUMC != null
                                    ? Row(
                                        children: <Widget>[
                                          SizedBox(
                                            width: 13,
                                          ),
                                          Text(
                                            'C.',
                                            style: answerC == true
                                                ? TextStyle(color: Colors.green)
                                                : TextStyle(color: Colors.red),
                                          ),
                                          SizedBox(
                                            width: 5,
                                          ),
                                          Text(
                                            '${c}',
                                            style: answerC == true
                                                ? TextStyle(color: Colors.green)
                                                : TextStyle(color: Colors.red),
                                          ),
                                        ],
                                      )
                                    : Row(
                                        children: <Widget>[
                                          SizedBox(
                                            width: 13,
                                          ),
                                          Text(
                                            'C.',
                                            style: TextStyle(
                                                color: Color.fromRGBO(
                                                    121, 128, 136, 1)),
                                          ),
                                          SizedBox(
                                            width: 5,
                                          ),
                                          Text(
                                            '${c}',
                                            style: TextStyle(
                                                color: Color.fromRGBO(
                                                    121, 128, 136, 1)),
                                          ),
                                        ],
                                      ),
                                onTap: () {
                                  if (kaiguan == false) {
                                    kaiguan = true;
                                    xuananswer = 'C';
                                    answerSUM = true;
                                    answerSUMC = true;
                                    if (answer == xuananswer) {
                                      answerC = true;
                                      rightanswer = rightanswer + 1;
                                    } else {
                                      answerC = false;
                                    }
                                  }
                                  setState(() {});
                                },
                              )),
                          SizedBox(
                            height: 10,
                          ),
                          Container(
                            width: 280,
                            height: 45,
                            alignment: Alignment.centerLeft,
                            decoration: BoxDecoration(
                              color: Color.fromRGBO(250, 251, 252, 1),
                              border: Border.all(
                                color: Color.fromRGBO(220, 220, 220, 1),
                              ),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(5)),
                            ),
                            child: InkWell(
                              child: answerSUMD != null
                                  ? Row(
                                      children: <Widget>[
                                        SizedBox(
                                          width: 13,
                                        ),
                                        Text(
                                          'D.',
                                          style: answerD == true
                                              ? TextStyle(color: Colors.green)
                                              : TextStyle(color: Colors.red),
                                        ),
                                        SizedBox(
                                          width: 5,
                                        ),
                                        Text(
                                          '${d}',
                                          style: answerD == true
                                              ? TextStyle(color: Colors.green)
                                              : TextStyle(color: Colors.red),
                                        ),
                                      ],
                                    )
                                  : Row(
                                      children: <Widget>[
                                        SizedBox(
                                          width: 13,
                                        ),
                                        Text(
                                          'D.',
                                          style: TextStyle(
                                              color: Color.fromRGBO(
                                                  121, 128, 136, 1)),
                                        ),
                                        SizedBox(
                                          width: 5,
                                        ),
                                        Text(
                                          '${d}',
                                          style: TextStyle(
                                              color: Color.fromRGBO(
                                                  121, 128, 136, 1)),
                                        ),
                                      ],
                                    ),
                              onTap: () {
                                if (kaiguan == false) {
                                  kaiguan = true;
                                  xuananswer = 'D';
                                  answerSUM = true;
                                  answerSUMD = true;
                                  if (answer == xuananswer) {
                                    answerD = true;
                                    rightanswer = rightanswer + 1;
                                  } else {
                                    answerD = false;
                                  }
                                }
                                setState(() {});
                              },
                            ),
                          ),
                          answerSUM == true
                              ? Container(
                                  margin: EdgeInsets.fromLTRB(0, 20, 0, 0),
                                  child: Text(
                                    '正确答案：${answer}',
                                    style:
                                        TextStyle(fontWeight: FontWeight.bold),
                                  ),
                                )
                              : Text(''),
                          TimerBuilder.periodic(Duration(seconds: 1),
                              builder: (context) {
                            return Text("${i++}",style: TextStyle(fontSize: 1,color: Colors.white),);
                          }),
                          SizedBox(
                            height: 30,
                          )
                        ],
                      ),
                    )
                  : JieWeiQu(rightanswer,i,title)
            ],
          )
        ],
      ),
    );
  }
}
